﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;
using Shrestha.Data;
using Shrestha.Data.Database;
using Shrestha.Data.EventModel;
using MySql.Data.MySqlClient;
using Microsoft.Maps.MapControl.WPF;
using LinqToTwitter;

namespace Shrestha.Charts
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            DataSet EventDataSet = new DataSet();
            DataSet MetaDataSet = new DataSet();

            //using (MySqlConnection mconn = new MySqlConnection("Data Source=127.0.0.1;Database=vis;User ID=ayush;Password=antimatter"))
            using (MySqlConnection mconn = new MySqlConnection("Data Source=192.168.0.11;Database=isvc;User ID=ayush;Password=antimatter"))
            {
                //String mquery = "select distinct Latitude, Longitude, EventDate, UnitName from war where Latitude is not null and longitude is not null";
                //String mquery = "select distinct Latitude, Longitude, EventDate, Lower(UnitName)  as UnitName, (CivilianKIA+FriendlyKIA+HostNationKIA+EnemyKIA) as gradient from war where Latitude is not null and longitude is not null and (UnitName = 'tf linebacker' OR UnitName = 'tf spartan' OR UnitName = 'tf longhorn' OR UnitName = 'tf guardian' OR UnitName='tf falcon') order by EventDate asc";

                //String mquery = "select distinct w.Latitude Latitude, w.Longitude Longitude, w.EventDate eventdate, Lower(w.UnitName) as UnitName from war w, " +
                //    "(select distinct unitname, count(*) c from war where unitname like 'TF%' and eventdate between '2009-10-1' and '2009-10-21' group by unitname order by c desc limit 7) p " +
                //    "where w.Unitname = p.Unitname and Latitude is not null and longitude is not null and eventdate between '2009-10-1' and '2009-10-21' ";// +
                    //"eventdate between '2009-10-1' and '2009-10-31' and " +
                    //"Eventdate > '2009-8-18' and Eventdate < '2009-8-22' and " +
                    //"Latitude > 34.7 and latitude < 35 and longitude < 70 and"+
                //"Latitude > 34.4";
                //eventdate between '2009-10-1' and '2009-10-31' and 
                //"(UnitName = 'tf lethal' OR UnitName = 'tf denali' OR UnitName = 'tf 3 geronimo' OR "+
                //"UnitName='tf white eagle' OR UnitName='tf protector' OR UnitName='tf korrigan' OR UnitName='tf yukon') " +
                //"(UnitName = 'TF White Eagle' OR UnitName = 'TF Spartan' OR UnitName = 'TF PALEHORSE' OR UnitName = 'TF DENALI' OR UnitName = 'TF Barge Matal' or unitname='TF GRYPHON') " +
                //" order by EventDate asc";

                //String mquery = "select distinct Latitude, Longitude, Eventdate, UnitName from war where (" +
                //    "Unitname='TF THOR' or " +
                //    "Unitname='TF Talon' or " +
                //    "Unitname='TF Gladius' or " +
                //    "Unitname='TF Palehorse' or " +
                //    "Unitname='TF steel' or " +
                //    "Unitname='TF lonestar' or " +
                //    "Unitname='TF lethal') and " +
                //    "eventdate between '2009-7-1' and '2009-8-31' and " +
                //    "Latitude > 32";

                //String mquery = "select distinct Latitude, Longitude, EventDate, UnitName, CivilianKIA as gradient from war where Latitude is not null and longitude is not null and (type='Explosive Hazard' or type='Enemy Action');";//  and CivilianKIA > 0";
                //String mquery = "select Latitude, Longitude, EventDate from slave";
                //String mquery = "select bLatitude as Latitude, blongitude as Longitude, eventdate, shipname as UnitName from slave where latitude is not null and longitude is not null and eventdate is not null";

                //String mquery = "select distinct Latitude, Longitude, EventDate, (CivilianKIA+FriendlyKIA+HostNationKIA+EnemyKIA) as gradient from war where Latitude is not null and longitude is not null and (type='Explosive Hazard' or type='Enemy Action') and EventDate between '2009-5-26' and '2009-12-31' and (CivilianKIA+FriendlyKIA+HostNationKIA+EnemyKIA) > 0 order by gradient asc";

                //String mquery = "select distinct Latitude, Longitude, EventDate, Type from war " +
                //    "where Latitude is not null and longitude is not null "+
                //    "and Latitude < 35 and Latitude > 34.945 and Longitude > 69 and Longitude < 70";// +
                    //"and Eventdate between '2009-3-1' and '2009-12-1' "+
                    //"and (type='Explosive Hazard')";

                //31.03955841, 65.7148819, 106


                //String mquery = "select distinct Latitude, Longitude, Eventdate,p.c gradient from war w join (select distinct Latitude la, Longitude lo, count(*) c from war where Latitude < 31.2 and Latitude > 30.8 and Longitude > 65 and Longitude < 66 group by la,lo order by c desc) p on w.latitude = p.la and w.longitude=p.lo";

                //String mquery = "select distinct Latitude, Longitude, Eventdate,p.c gradient from war w join (select distinct Latitude la, Longitude lo, count(*) c from war where Latitude < 34.9542 and Latitude > 34.945 and Longitude > 69.2442 and Longitude < 69.2986 group by la,lo order by c desc) p on w.latitude = p.la and w.longitude=p.lo";

                //String mquery = "select distinct Latitude, Longitude, EventDate, Type from war where Latitude is not null and longitude is not null and eventdate is not null and latitude > 30 and latitude < 36 and longitude > 60 and longitude < 72 and EventDate between '2009-6-15' and '2009-10-15'";
                //and ( type='Enemy Action' or type='Explosive Hazard')";
                //Typeofunit='CF'  or type='Friendly Action' or type='Non-Combat Event'  and ( type='Enemy Action' or type='Explosive Hazard')

                //String mquery = "select distinct Latitude, Longitude, EventDate from war where Latitude is not null and longitude is not null and eventdate is not null";

                //String mquery = "select distinct latitude, longitude, eventdate, CivFatalities as gradient from lra where civfatalities > 10 order by gradient";

                //String mquery = "select distinct latitude, longitude, eventdate, (CivFatalities+CivInjured) as gradient from lra";

                //String mquery = "select distinct latitude, longitude, eventdate, (BombsDropped) as gradient from laos where latitude > 17.17 and latitude < 17.55 and longitude between 105.6 and 106.2 and eventdate between '1970-9-30' and '1971-2-25'";

                //String mquery = "select distinct Latitude, Longitude, EventDate from diary where Latitude is not null and longitude is not null and (type='Explosive Hazard' or type='Enemy Action') ";

                //String mquery = "select eventdate, latitude, longitude from testcircle";

                
                 //and (shipname='Aurora' || shipname='Diana' || shipname='Venus'|| shipname='Hope' || shipname='Betty' || shipname='Union')
                //String mquery = "Select distinct Latitude, Longitude, EventDate, CivFatalities+CivInjured as gradient, Clash from lra where CivFatalities+CivInjured > 0";

                /// Queries For Vissoft 2013
                
                string mquery = null;
                string head = "select distinct a.latitude Latitude, a.longitude Longitude, b.date EventDate from ";
                string tail = " b left join (select * from authorv) a on a.author=b.author where latitude is not null and longitude is not null and b.date is not null";
                
                /// For d3 
                mquery = head + "h_d3" + tail;

                /// For Rails
                mquery = head +"h_rails" + tail;

                /// For HomeBrew
                mquery = head +"h_homebrew" + tail;

                /// For node.js
                mquery = head + "h_node" + tail;

                /// For jquery
                mquery = "select h.eventdate EventDate, l.latitude Latitude, l.longitude Longitude from h_jquery h left join h_jquery_location l on h.user = l.user where l.latitude is not null and l.longitude is not null";

                /// For apache
                mquery = "select h.eventdate EventDate, l.latitude Latitude, l.longitude Longitude from h_apache h left join h_apache_location l on h.user = l.user where l.latitude is not null and l.longitude is not null";


                MySqlDataAdapter mAdapter = new MySqlDataAdapter(mquery, mconn);
                mAdapter.Fill(EventDataSet, "war");

                //mquery = "select * from lra_meta;";
                //mquery = "select * from war_meta;";
                //mAdapter = new MySqlDataAdapter(mquery, mconn);
                //mAdapter.Fill(MetaDataSet, "meta");
            }

            String lat, lng, date, character, gradient;
            lat = "Latitude"; lng = "Longitude"; date = "EventDate";
            character = "UnitName"; 
            gradient="gradient" ;

            //Runner r = new Runner(EventDataSet, lat, lng, date,character, null);
            
            //Runner r = new Runner(EventDataSet, MetaDataSet, lat, lng, date, null, null);

            //Runner r = new Runner(EventDataSet, lat, lng, date, null, null, gradient);

            Runner r = new Runner(EventDataSet, lat, lng, date, null, null);
            //DataView dv = r.GetCondition("Math.Round(Latitude) = 3 and round(Longitude)=29");

            //StoryWindow w = new StoryWindow(r){
            //    Height = 500,
            //    Width = 1000
            //};

            ChartWindow w = new ChartWindow(r)
            {
                Height = 700,
                Width = 600
            };
            //MapWindow w2 = new MapWindow(r);
            w.Show();
            //w2.Show();
            
        }
    }
}
